﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuaManSys.Common;
using QMSDAL;

namespace QuaManSys.InnerInfo
{
    public partial class InnerInfoReminderBox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] != null)
            {
                //根据当前用户的身份显示消息提醒栏
                CurUser user = (CurUser)Session["CurUser"];
                ShowReminderList(user);
            }
            else
            {
                //跳转到登录页面
                Response.Redirect("~/Login.aspx");
            }
        }
        #region 根据当前登录用户的权限显示消息栏
        private void ShowReminderList(CurUser user)
        {
            string emId = user.employeeId;
            //根据emID查询雇员角色
            //string empRoles = "A";
            string dept = user.department;  //雇员部门

            //查询各状态需要处理的信息表编号列表
            //List<string> listInfoFil = Permission.QueryInnerInfoStatus("归档");

            //查询当前用户对内部信息录入和内部收件箱具有的操作权限
            List<string> listOwnedReg = Permission.QueryOpers(emId, "1A");
            List<string> listOwnedRem = Permission.QueryOpers(emId, "1B");
            List<string> listDeptReg = Permission.QueryOpers(dept, "1A");
            List<string> listDeptRem = Permission.QueryOpers(dept, "1B");
            //需要重新登记的信息
            #region 重新登记的消息

            if (listOwnedReg.Contains("A"))
            {
                List<InnerQualityInfo> listInfoReg = Reminder.QueryInReminderList("登记", user);
                if (listInfoReg.Count != 0)
                {
                    int i = 1;
                    string proUrl = "InnerInfoProcessing.aspx";
                    pnlReminder1.GroupingText = "以下表需要您完善信息";
                    foreach (var item in listInfoReg)
                    {
                        FillPanelWithControl(pnlReminder1, i++, item.qualityInfoId, "登记", proUrl);
                    }
                }
                else
                {
                    //todo:如果没有需要重新登记的信息
                }
            }
            #endregion
            //待分发的信息
            #region 待分发的消息

            if (listOwnedRem.Contains("B0") || listDeptRem.Contains("B0"))
            {
                List<InnerQualityInfo> listInfoDis = Reminder.QueryInReminderList("分发", user);
                if (listInfoDis.Count != 0)
                {
                    pnlReminder2.GroupingText = "待分发列表";
                    int i = 1;
                    string disUrl = "InnerInfoProcessing.aspx";
                    foreach (var item in listInfoDis)
                    {
                        FillPanelWithControl(pnlReminder2, i++, item.qualityInfoId, "分发", disUrl);
                    }

                }
            }
            #endregion
            //待处理的消息
            #region 待处理的消息

            if (listOwnedRem.Contains("B1") || listDeptRem.Contains("B1"))
            {
                List<InnerQualityInfo> listInfoPro = Reminder.QueryInReminderList("处理", user);
                if (listInfoPro.Count != 0)
                {
                    pnlReminder3.GroupingText = "待处理列表";
                    int i = 1;
                    string disUrl = "InnerInfoProcessing.aspx";
                    foreach (var item in listInfoPro)
                    {
                        FillPanelWithControl(pnlReminder3, i++, item.qualityInfoId, "处理", disUrl);
                    }

                }
            }
            #endregion
            //待归档的信息
            #region 归档的消息

            if (listOwnedRem.Contains("D1") || listDeptRem.Contains("D1"))
            {
                List<InnerQualityInfo> list = Reminder.QueryInReminderList("归档", user);
                if (list.Count != 0)
                {

                    pnlReminder4.GroupingText = "待归档列表";
                    int i = 1;
                    string disUrl = "InnerInfoProcessing.aspx";
                    foreach (var item in list)
                    {
                        FillPanelWithControl(pnlReminder4, i++, item.qualityInfoId, "归档", disUrl);
                    }

                }
            }
            #endregion
        }
        #endregion

        #region FillPanelWithControls
        /// <summary>
        /// 根据数据库里的记录，自动在panel生成仅显示信息编号的列表
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="i"></param>
        /// <param name="infoId"></param>
        private void FillPanelWithControl(Panel panel, int i, string infoId,
            string needTo, string url)
        {
            Label myLabel = new Label();
            panel.Controls.Add(myLabel);
            myLabel.Text = (i++).ToString() + " . 质量信息编号为：" +
                infoId + "需要" + needTo + "。";

            HyperLink innerHref = new HyperLink();
            panel.Controls.Add(innerHref);
            innerHref.Text = "立即处理。";
            innerHref.NavigateUrl = url + "?id=" + infoId + "&state=" + needTo;
            panel.Controls.Add(new LiteralControl("<br />"));
        }
        #endregion
    }
}